package com.dwxt.cdms.util;

import com.dwxt.cdms.entity.base.GlobalSystemParam;
import com.dwxt.cdms.service.GlobalSystemParamService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 系统参数工具类
 *
 * @author Kuang.JiaZhuo
 * @date 2020-09-11 12:08
 */
@Component
public class ParamUtil {

    private static GlobalSystemParamService globalSystemParamService;
    private static Map<String, String> globalParamMap;
    private static Map<String, String> globalParamOnIdMap;

    @Autowired
    public void setGlobalSystemParamService(GlobalSystemParamService globalSystemParamService) {
        ParamUtil.globalSystemParamService = globalSystemParamService;
    }

    public static void getGlobalSystemParamListAll() {
        globalParamMap = Maps.newHashMap();
        globalParamOnIdMap = Maps.newHashMap();
        List<GlobalSystemParam> globalParams = globalSystemParamService.selectListAll();
        for (GlobalSystemParam globalParam : globalParams) {
            globalParamMap.put(globalParam.getParamCode(), globalParam.getParamValue());
            globalParamOnIdMap.put(globalParam.getParamCode(), globalParam.getId());
        }
    }

    public static String getValue(String code) {
        if(globalParamMap == null ) {
            getGlobalSystemParamListAll();
        }
        return globalParamMap.get(code);
    }

    public static String getId(String code) {
        if(globalParamOnIdMap == null ) {
            getGlobalSystemParamListAll();
        }
        return globalParamOnIdMap.get(code);
    }
}
